package com.gitee.xmhzzz.component.spring.cloud.component.dao.utils;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.TableFieldInfo;

import java.util.Objects;
import java.util.function.Predicate;

/**
  * @ClassName
  * @Description
  * @Author wzq
  * @Date 2023/12/15 16:52
  * @Version 1.0
  */
public class MybatisPlusWrapperSelectUtil {
    /**
     * 只返回指定字段
     *
     * @param fieldValues  需要返回的字段名称数组
     * @param queryWrapper queryWrapper
     * @param clazz        字段所属类对象
     */
    public static <T> void fieldValues(String[] fieldValues, LambdaQueryWrapper<T> queryWrapper, Class<T> clazz) {
        if (Objects.isNull(fieldValues) || fieldValues.length <= 0) {
            return;
        }

        Predicate<TableFieldInfo> predicate = null;
        for (String field : fieldValues) {
            predicate = predicate == null ? p -> p.getColumn().equals(field) : predicate.or(p -> p.getColumn().equals(field));
        }
        queryWrapper.select(clazz, predicate);
    }
}
